<?php 

if (isset($_POST['logout']))
    $_SESSION = array();

$login = "<form class='navbar-form pull-right'><input class='span2' id='Email' type='text' placeholder='Email'><input class='span2' id='Password' type='password' placeholder='Password'><button type='submit' id='login' class='btn' data-toggle='popover' data-placement='bottom' data-content='Sign in error.  Please re-enter your email and password.'>Sign in</button></form>";

include('db_config.inc.php');

if (isset($_SESSION['user_name'])) {
    $user = $_SESSION['user_name'];
    echo "<div class='container-fluid nav-collapse'><p class='navbar-text pull-right'>Welcome $user&nbsp;&nbsp;&nbsp;&nbsp;<a href='logout' id='logout'>Sign out</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='reservations.php'>My Reservations</a></p></div>";
} elseif (isset($_POST['email']) && isset($_POST['pwd'])) {

    $email = $_POST['email'];
    $pwd = $_POST['pwd'];
    $mysqli = mysqli_connect($mysqlhost, $mysqluser, $mysqlpassword, $mysqldb);


    $query = "SELECT * FROM Customers WHERE customerEmail='$email' AND password='$pwd'";
    $result = mysqli_query($mysqli, $query) or die(mysqli_error());
    $num_row = mysqli_num_rows($result);
    $row = mysqli_fetch_array($result);
    if ($num_row >= 1) {
        $_SESSION['user_name'] = $row['customerEmail'];
        $_SESSION['customerId'] = $row['customerId'];
        $user = $_SESSION['user_name'];
        echo "<div class='container-fluid nav-collapse'><p class='navbar-text pull-right'>Welcome $user&nbsp;&nbsp;&nbsp;&nbsp;<a href='logout' id='logout'>Sign out</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='reservations.php'>My Reservations</a></p></div>";
    } else {
        echo $login;
    }
}
else
    echo "<!-- not logged in --><form class='navbar-form pull-right'><input class='span2' id='Email' type='text' placeholder='Email'><input class='span2' id='Password' type='password' placeholder='Password'><button type='submit' id='login' class='btn' data-toggle='popover' data-placement='bottom' data-content='Sign in error.  Please re-enter your email and password.'>Sign in</button></form>";
?>
